layui.use(['element', 'layer', 'form'], function () {
    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块

    // 设置弹窗登录框
    var $ = layui.jquery,
        layer = layui.layer; //独立版的layer无需执行这一句

    //表单
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
    //目录选择
    $(".left-Navigation .left-menu").find("li").click(function () {
        $(".left-Navigation .left-menu").find("a").removeClass("active");
        $(this).find("a").toggleClass("active");
        $(".learn-main .main-title").find("span").text($(this).text());
        $(".main-list ul").empty();
        count($(".left-menu .active").attr("value"));
        change(1, 12, $(".left-menu .active").attr("value"));
    });
    $(".left-Navigation .detail-left-menu").find("li").click(function () {
        $(".left-Navigation .detail-left-menu").find("a").removeClass("active");
        $(this).find("a").toggleClass("active");
    });
    //初始化前后切换
    changetip();

    //前后切换
    function changetip() {
        var listnum = $(".left-Navigation .detail-left-menu li").length - 1;
        $(".change-title #next").find("span").text($(".left-Navigation .detail-left-menu li").eq(1).text());
        $(".left-Navigation .detail-left-menu").find("li").click(function () {
            if ($(this).index() == "0") {
                $(".change-title #next").find("span").text($(this).next().find("a").text());
                $(".change-title #pre").hide();
                $(".change-title #next").show();
            } else if ($(this).index() == listnum) {
                $(".change-title #pre").find("span").text($(this).prev().find("a").text());
                $(".change-title #pre").show();
                $(".change-title #next").hide();
            } else {
                $(".change-title #pre").find("span").text($(this).prev().find("a").text());
                $(".change-title #next").find("span").text($(this).next().find("a").text());
                $(".change-title #pre").show();
                $(".change-title #next").show();
            }
        });
    }

    initactive();

    function initactive() {
        $(".left-menu").find("li").eq(0).find("a").addClass("active");
        $(".detail-left-menu").find("li").each(function () {
            console.log($(this).attr("value"));
            if ($("#theid").val() == $(this).find("a").attr("value")) {
                console.log("是");
                $(".detail-left-menu").find("li").find(".active").removeClass("active");
                $(this).find("a").addClass("active");
                chosetip();
            }
        })

    }

    //分页是否显示
    function count(type) {
        var type = $(".left-menu .active").attr("value");
        $.get("/Learn/count",
            {
                type: type
            },
            function (data) {
                //加载后台返回的List集合数据
                console.log(data);
                if (data < 12) {
                    $("#learn-list").hide();
                }
                else {
                    $("#learn-list").show();
                }
            },
            "json"
        );
    }

    //分页
    function change(start, limit) {
        var type = $(".left-menu .active").attr("value");
        $.get("/Learn/getdata",
            {
                start: start,
                limit: limit,
                type: type
            },
            function (data) {
                //加载后台返回的List集合数据
                console.log(data);
                // $(".main-list ul").append();
                for (var o in data) {
                    $(".main-list ul").append(" " +
                        "<li onclick='gotodetail(this);'>" +
                        "<input id='lea-id' type='hidden' value=" + data[o]["ldid"] + " >" +
                        "<span id='lea-tip' >" + data[o]["title"] +
                        "</span>" +
                        "<div class='lea-con'>" +
                        " <span >" + data[o]["content"] + "</span></div></li>");
                }
            },
            "json"
        );
    }

    layui.use('laypage', function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'learn-list' //注意，这里的 test1 是 ID，不用加 # 号
            ,
            count: $("#count").val() //数据总数，从服务端得到
            ,
            limit: 12,
            jump: function (obj, first) {
                if (!first) {
                    $(".main-list ul").empty();
                    change(obj.curr, obj.limit);
                }
            }
        });
    });

    //进入学习资料详情页面
    gotodetail = function (obj) {
        var id = $(obj).find("#lea-id").val();
        var type = $(".left-menu .active").attr("value");
        window.location.href = "/Learn/detail/" + type + "/" + id;
    };

    //选择学习资料详
    selectdetail = function (obj) {
        var id = $(obj).attr("value");
        $.ajax({
            url: "/Learn/selectdetail",
            data: {"id": id},
            success: function (data) {
                $(".learn-text").find("h3").text(data.title);
                $(".learn-text").find("span").text(data.content);
                $("#theid").val(data.ldid);
            }
        });
    };
    //前后翻页
    $("#pre").click(function () {
        var id = $(".detail-left-menu .active").parent().prev().find("a");
        $(".left-Navigation .detail-left-menu").find("a").removeClass("active");
        id.toggleClass("active");
        selectbytitle(id.attr("value"));
    });
    $("#next").click(function () {
        var id = $(".detail-left-menu .active").parent().next().find("a");
        $(".left-Navigation .detail-left-menu").find("a").removeClass("active");
        id.toggleClass("active");
        selectbytitle(id.attr("value"));
    });

    function selectbytitle(id) {
        var type = $("#thetype").attr("value");
        chosetip();
        $.ajax({
            url: "/Learn/selectbytitle",
            data: {"id": id},
            success: function (data) {
                $(".learn-text").find("h3").text(data.title);
                $(".learn-text").find("span").text(data.content);
            }
        });
    }

    function chosetip() {
        var prev = $(".detail-left-menu .active").parent().prev().find("a");
        var next = $(".detail-left-menu .active").parent().next().find("a");
        if (prev.length > 0) {
            $("#pre").find("span").text(prev.text());
            $(".change-title #pre").show();
        } else {
            $(".change-title #pre").hide();
            $(".change-title #next").show();
        }
        if (next.length > 0) {
            $("#next").find("span").text(next.text());
            $(".change-title #next").show();
        } else {
            $(".change-title #pre").show();
            $(".change-title #next").hide();
        }
    }

    //返回顶部
    $(window).scroll(function () {
        //为了保证兼容性，这里取两个值，哪个有值取哪一个
        //scrollTop就是触发滚轮事件时滚轮的高度
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;

        // console.log("滚动距离" + scrollTop);
        // 由于未知的原因,如果写在一起或导致页面从底部向上滚动过快时右侧导航栏无法触发向下移动,所以分开写

        // 右侧导航栏
        var height = scrollHeight - clientHeight;
        if (scrollTop < height - 200) {
            $("#totop").stop().animate({
                top: '600px'
            }, 400);
        } else if (scrollTop > height - 200) {
            $("#totop").stop().animate({
                top: '300px'
            }, 400);
        }
    });
    $("#totop").click(function () {
        timer = setInterval(function () {
            //获取滚动条的滚动高度
            var osTop = document.documentElement.scrollTop || document.body.scrollTop;
            //用于设置速度差，产生缓动的效果
            var speed = Math.floor(-osTop / 6);
            document.documentElement.scrollTop = document.body.scrollTop = osTop + speed;
            isTop = true; //用于阻止滚动事件清除定时器
            if (osTop == 0) {
                clearInterval(timer);
            }
        }, 30);
    });
});
